package thread2;

import java.util.concurrent.*;

public class RunnableFutureTaskExample {
    public static void main(String[] args) {
        // 创建一个 Runnable 任务
        Runnable runnable = () -> {
            System.out.println("任务开始执行...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        // 创建 FutureTask，指定结果值
        FutureTask<String> futureTask = new FutureTask<>(runnable, "任务完成");

        // 创建线程并启动任务
        Thread thread = new Thread(futureTask);
        thread.start();

        try {
            // 获取任务结果
            String result = futureTask.get();
            System.out.println("任务执行结果: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}